package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.constants.CertificationConstants;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    //认证审核服务
    @Resource
    private IWorkerCertificationAuditService workerCertificationAuditService;


    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 添加服务人员认证信息
     *
     * @param workerCertificationAuditAddReqDTO 服务人员认证信息
     */
    @Override
    @Transactional
    public void addCertification(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {
        //1 保存认证信息
        WorkerCertification workerCertification = new WorkerCertification();
        BeanUtil.copyProperties(workerCertificationAuditAddReqDTO, workerCertification);
        //1.1 设置认证状态
        workerCertification.setCertificationStatus(CertificationConstants.CERTIFICATION_STATUS_AUDITING);
        //1.2设置id
        workerCertification.setId(UserContext.currentUserId());
        //删除原来的，然后新增
        if (super.getById(UserContext.currentUserId()) != null) {
            super.removeById(UserContext.currentUserId());
        }
        super.save(workerCertification);

        //2 保存认证审核信息
        WorkerCertificationAudit workerCertificationAudit = new WorkerCertificationAudit();
        BeanUtils.copyProperties(workerCertificationAuditAddReqDTO, workerCertificationAudit);
        //2.1 设置服务人员认证id
        workerCertificationAudit.setServeProviderId(workerCertification.getId());
        //2.2设置审核状态
        workerCertificationAudit.setAuditStatus(CertificationConstants.AUDIT_STATUS_NOT_AUDIT);
        //2.3 设置认证状态
        workerCertificationAudit.setCertificationStatus(CertificationConstants.AUDIT_STATUS_AUDITED);
        workerCertificationAuditService.save(workerCertificationAudit);
    }
}
